import {
  CallHandler,
  ExecutionContext,
  Injectable,
  NestInterceptor,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Observable, tap } from 'rxjs';

@Injectable()
export class LogTimeInterceptor implements NestInterceptor {
  constructor(private configService: ConfigService) {}
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    console.log(
      `before x-api-token=${this.configService.get('API_ACCESS_TOKEN_HEADER')}`,
    );
    const now = Date.now();
    return next.handle().pipe(
      tap((...args) => {
        console.log(args);
        console.log(`After... ${Date.now() - now}ms`);
      }),
    );
  }
}
